package top.yeyusmile.service;

import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.yeyusmile.common.MyTemplate;
import top.yeyusmile.common.TypeConstant;
import top.yeyusmile.pojo.MyQQMessage;
import top.yeyusmile.pojo.ReceiveFile;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 夜雨
 * @Web www.yeyusmile.top
 * @date 2022/3/18 9:48
 */
@Slf4j
@Service
public class OtherServiceImpl implements OtherService {

    @Autowired
    private SendMsgService sendMsgService;

    @Autowired
    private MyTemplate myTemplate;

    @Override
    public void alertGroupFileReceive(MyQQMessage message) {
        ReceiveFile receiveFile = new Gson().fromJson(message.getMqMsg(), ReceiveFile.class);
        String msg = message.getMqFromqq() + "上传了文件：\n" +
                "文件id:" + receiveFile.getFileId() + "\n" +
                "文件名:" + receiveFile.getFileName() + "\n" +
                "文件大小:" + receiveFile.getFileSize();
        sendMsgService.sendMsg2Group(msg, message);
    }

    @Override
    public void alterQuitGroup(MyQQMessage message) {
        String msg = "";
        if (message.getMqType() == TypeConstant.MSGTYPE_ONE_QUIT_GROUP) {
            msg = "提醒：" + message.getMqFromqq() + "已经退出群聊";
        }

        if (message.getMqType() == TypeConstant.MSGTYPE_ONE_BE_QUIT) {
            msg = "提醒：" + message.getMqPassiveqq() + "被移出群聊";
        }

        sendMsgService.sendMsg2Group(msg, message);
    }

    /**
     * 发送xml音乐卡片消息
     *
     * @param title   标题
     * @param summary 描述
     */

    private void sendIPCard(String title, String summary, MyQQMessage myQQMessage) {
        String xmlMsg = String.format("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><msg serviceID=\"2\" templateID=\"1\" action=\"web\" brief=\"Music\" sourceMsgId=\"0\" url=\"http://yeyusmile.top\" flag=\"0\" adverSign=\"0\" multiMsgFlag=\"0\"><item layout=\"2\"><audio cover=\"http://www.kangzuo.top/kp/kp.php\" src=\"http://www.kangzuo.top/kp/kp.php\" /><title>%s</title><summary>%s</summary></item><source name=\"\" icon=\"http://www.kangzuo.top/kp/kp.php\" action=\"\" appid=\"-1\" /></msg>", title, summary);
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("c1", myQQMessage.getMqRobot());
        params.put("c2", 0);
        params.put("c3", myQQMessage.getMqType());
        params.put("c4", myQQMessage.getMqFromid());
        params.put("c5", myQQMessage.getMqFromqq());
        params.put("c6", xmlMsg);
        myTemplate.sendXml(params);

    }

    @Override
    public void ip(MyQQMessage message) {
        String title  = message.getMqMsg().replaceAll("ip探针", "").trim();
        sendIPCard(title, "by 夜雨", message);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        sendMsgService.sendMsg("卡片已经生成,请发给别人,ip查看地址：http://www.kangzuo.top/kp/kp.php", message);
    }

    @Override
    public void alterGroupChange(MyQQMessage message) {
        sendMsgService.sendMsg2Group("提醒：" + message.getMqFromqq() + "修改了群名片", message);
    }
}
